<?php

/**
 * 自定义日志写入
 *
 * @param string $content 写入内容
 * @param string $dir 文件路径
 * @param string $title 日子标题
 */
if (!function_exists('fish_logger')) {
    function fish_logger($content, $dir = "storage/logger", $title = "日志写入")
    {
        $file = fish_create_date_path($dir);
        $time = date("Y-m-d H:i:s");
        $content = "----------【{$title}】-----------\n[{$time}] {$content}\n---------------------\n";
        file_put_contents($file, $content, FILE_APPEND);
    }
}

/**
 * 生成日期对应的文件路劲
 *
 * @param $date string 日期
 * @param $path
 * @return string
 */
if (!function_exists('fish_create_date_path')) {
    function fish_create_date_path($path = null)
    {
        /*动态参数 -- 根目录*/
        $rootPath = public_path();
        
        $path = $path ? $path . '/' : "";
        $date = date("Ymd");
        $path = $rootPath . $path . substr($date, 0, 4) . '/' . substr($date, 4, 2);
        if (!file_exists($path)) fish_create_dir($path);
        $fileName = substr($date, 6, 2);
        $path = "{$path}/{$fileName}.txt";
        return $path;
    }
}

/**
 * 递归创建文件
 *
 * @param $dir string 路径
 * @param int $mode int 权限
 */
if (!function_exists('fish_create_dir')) {
    function fish_create_dir($dir, $mode = 0755)
    {
        mkdir($dir, $mode, true);
    }
}